package com.work.libtest2.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.dto.JobDto;
import com.work.libtest2.entity.Department;
import com.work.libtest2.entity.Employee;
import com.work.libtest2.entity.Job;
import com.work.libtest2.mapper.DepartmentMapper;
import com.work.libtest2.mapper.JobMapper;
import com.work.libtest2.service.JobService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: JobServiceImpl
 * Package: com.work.libtest2.service.impl
 * Description:
 *
 * @Author
 * @Create 2024/6/10 9:12
 * @Version 1.0
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {

    @Resource
    private JobMapper jobMapper;

    @Resource
    private DepartmentMapper deptMapper;

    @Override
    public List<Job> getAllJobName() {
        return jobMapper.selectList(null);
    }

    @Override
    public IPage<JobDto> getPage(Integer currentPage, Integer pageSize, JobDto jobDto) {
        LambdaQueryWrapper<Job> lqw = new LambdaQueryWrapper<>();
        lqw.like(jobDto.getDeptId()!=null,Job::getDeptId,jobDto.getDeptId());
        lqw.like(jobDto.getJobId()!=null,Job::getJobId,jobDto.getJobId());
        IPage page = new Page(currentPage,pageSize);
        jobMapper.selectPage(page,lqw);

        List<JobDto> list = new ArrayList<>();
        IPage newPage = new Page();
        BeanUtils.copyProperties(page,newPage);
        newPage.setRecords(list);

        for (int i = 0; i < page.getRecords().size(); i++) {
            JobDto newJobDto = new JobDto();
            BeanUtils.copyProperties(page.getRecords().get(i),newJobDto);
            Department department = deptMapper.selectById(newJobDto.getDeptId());
            newJobDto.setDeptName(department.getDeptName());
            list.add(newJobDto);
        }
        return newPage;
    }

    @Override
    public int addJob(Job job) {
        return jobMapper.insert(job);
    }

    @Override
    public Job getJobById(Integer jobId) {
        return jobMapper.selectById(jobId);
    }

    @Override
    public int updateJob(Job job) {
        return jobMapper.updateById(job);
    }

    @Override
    public int deleteJobById(Integer jobId) {
        return jobMapper.deleteById(jobId);
    }
}
